<?php

namespace fast;

use think\Db;

use think\Exception;
use fast\Http;
use fast\WechatMini;


/**
 * Http 请求类
 */
class GoodsAdd
{

  
       
    public static function addData($data)
    {
            


        $insdata['category_id'] = [];
        $insdata['goods_name'] = $data['goods_name'];
        $insdata['desc'] = $data['desc'];


        if (isset($data['category_id1']) && !empty($data['category_id1'])) {
            $map['pid'] = 0;
            $map['name'] = $data['category_id1'];
            $fenlei = Db::table('fa_litestore_category')->where($map)->find();
            if (!empty($fenlei)) {
                array_push($insdata['category_id'],$fenlei['id']);
            }
        }

        if (isset($data['category_id2']) && !empty($data['category_id2'])) {
            $map2['pid'] = ['<>',0];
            $map2['name'] = $data['category_id2'];
            $fenlei2 = Db::table('fa_litestore_category')->where($map2)->find();
            if (!empty($fenlei2)) {
                array_push($insdata['category_id'],$fenlei2['id']);
            }
        }
        if (!empty($data['goods_sort'])) {
           $insdata['goods_sort'] = $data['goods_sort'];
        }

        if (empty($insdata['category_id'])) {
           $insdata['category_id'] = '0';
        }else{
            $insdata['category_id'] = implode(',',$insdata['category_id']);
        }


        $spc['danwei'] = $data['danwei'];
        $spc['goods_no'] = $data['goods_no'];
        $spc['goods_price'] = $data['goods_price'];
        $spc['goods_price2'] = $data['goods_price2'];
        $spc['goods_price4'] = $data['goods_price4'];
        $spc['goods_price4'] = $data['goods_price4'];



        Db::startTrans();
        try {
            //商品编号不为空
            if (!empty($data['goods_no'])) {
                $map_spc['goods_no'] = $data['goods_no'];
                $spc_res = Db::table('fa_litestore_goods_spec')->where($map_spc)->find();

                //没有找到对应的商品
                if (empty($spc_res)) {
                    $insdata['createtime'] = time();
                    $insdata['updatetime'] = time();
                    $insdata['goods_status'] = '20';
                    $goods_res = Db::table('fa_litestore_goods')->insertGetId($insdata);

                    $spc['goods_id'] = $goods_res;
                    $spc['create_time'] = time();
                    $spc['update_time'] = time();
                    Db::table('fa_litestore_goods_spec')->insertGetId($spc);

                }else{
                    //有对应的商品
                    $map_goods['goods_id'] = $spc_res['goods_id'];
                    $insdata['updatetime'] = time();
                    Db::table('fa_litestore_goods')->where($map_goods)->update($insdata);
                    $map_spc['goods_no'] = $data['goods_no'];
                    Db::table('fa_litestore_goods_spec')->where($map_spc)->update($spc);
                }

            }else{
                //商品编号为空
                $insdata['createtime'] = time();
                $insdata['updatetime'] = time();
                $insdata['goods_status'] = '20';
                $goods_res = Db::table('fa_litestore_goods')->insertGetId($insdata);
                $spc['goods_id'] = $goods_res;
                $spc['create_time'] = time();
                $spc['update_time'] = time();
                Db::table('fa_litestore_goods_spec')->insertGetId($spc);
            }
            Db::commit();
            return 1;
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            logs('GoodsAdd','添加报错--添加信息---------'.json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            logs('GoodsAdd','添加报错--报错信息---------'.$e->getMessage());
            return 0;
           
        }

       


        // logs('wechat_order','发货信息_______'.$sysParams);
        // logs('wechat_order','订单返回_______'.json_encode($response,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
     

    }
    




}
